package com.yami.shop.sys.api.controller;

import com.yami.shop.common.bean.LangConfig;
import com.yami.shop.common.bean.LangItemConfig;
import com.yami.shop.common.bean.SysPayConfig;
import com.yami.shop.common.bean.SysServiceConfig;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.manager.impl.LangManager;
import com.yami.shop.service.SysConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 系统配置信息
 * @author yami
 */
@RestController
@Tag(name = "系统配置信息")
@RequestMapping("/sys/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private ShopConfig shopConfig;

    @Autowired
    private LangManager langManager;

    @Operation(summary =  "获取服务条款信息", description = "获取服务条款信息")
    @GetMapping("/info/serviceTerms")
    public ServerResponseEntity<String> serviceTerms(){
        Integer dbLang = I18nMessage.getDbLang();
        if(Objects.equals(LanguageEnum.LANGUAGE_EN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue("SERVICE_TERMS_CONFIG_EN"));
        }
        return ServerResponseEntity.success(sysConfigService.getValue("SERVICE_TERMS_CONFIG_CN"));
    }

    @Operation(summary =  "获取隐私策略信息", description = "获取隐私策略信息")
    @GetMapping("/info/servicePolicy")
    public ServerResponseEntity<String> servicePolicy(){
        Integer dbLang = I18nMessage.getDbLang();
        if(Objects.equals(LanguageEnum.LANGUAGE_ZH_CN.getLang(), dbLang)) {
            return ServerResponseEntity.success(sysConfigService.getValue("SERVICE_POLICY_CONFIG_CN"));
        }
        return ServerResponseEntity.success(sysConfigService.getValue("SERVICE_POLICY_CONFIG_EN"));
    }

    @Operation(summary =  "获取系统支付开关", description = "获取系统支付开关")
    @GetMapping("/info/getSysPaySwitch")
    public ServerResponseEntity<SysPayConfig> getSysPaySwitch(){
        SysPayConfig sysPayConfig = sysConfigService.getSysConfigObject(Constant.PAY_SWITCH_CONFIG, SysPayConfig.class);
        return ServerResponseEntity.success(sysPayConfig);
    }

    @Operation(summary =  "获取系统服务条例开关", description = "获取系统服务条例开关")
    @GetMapping("/info/getSysServiceSwitch")
    public ServerResponseEntity<SysServiceConfig> getSysServiceSwitch() {
        SysServiceConfig sysServiceConfig = sysConfigService.getSysConfigObject(Constant.SERVICE_SWITCH_CONFIG, SysServiceConfig.class);
        return ServerResponseEntity.success(sysServiceConfig);
    }
    @GetMapping("/lang")
    @Operation(summary =  "国际化语言配置信息", description = "国际化语言配置信息")
    public ServerResponseEntity<LangConfig> getLangConfig(){
        LangConfig langConfig = langManager.getLangConfig();
        List<LangItemConfig> langItemConfigList = langConfig.getLangItemList().stream().filter(langItemConfig -> Objects.equals(langItemConfig.getLang(), langConfig.getLang())).collect(Collectors.toList());
        langConfig.getLangItemList().removeAll(langItemConfigList);
        langItemConfigList.addAll(langConfig.getLangItemList());
        langConfig.setLangItemList(langItemConfigList);
        return ServerResponseEntity.success(langConfig);
    }
    @Operation(summary =  "协议", description = "协议")
    @GetMapping("/info/servicePolicyAndTerms")
    public ServerResponseEntity<String> servicePolicyAndTerms(Integer type){
        Integer dbLang = I18nMessage.getDbLang();
        String key = null;
        if(Objects.equals(LanguageEnum.LANGUAGE_ZH_CN.getLang(), dbLang)) {
            switch (type) {
                case 1: key = "HWJ_REGISTER_RISK_TERMS_CONFIG_CN"; break; //海文交注册风险提示
                case 2: key = "HWJ_REGISTER_PRIVACY_TERMS_CONFIG_CN"; break;//海文交注册隐私条款
                case 3: key = "HWJ_OPENING_AGREEMENT_TERMS_CONFIG_CN"; break;//海文交注册开户协议
                case 4: key = "REAL_NAME_AGREEMENT_TERMS_CONFIG_CN"; break;//用户实名协议
                case 5: key = "SERVICE_POLICY_CONFIG_CN"; break;//用户实名隐私政策
                case 6: key = "TRANSFER_AGREEMENT_TERMS_CONFIG_CN"; break;//划转协议
                case 7: key = "INCREASE_BY_CONVERSION_TERMS_CONFIG_CN"; break;//转赠协议
                case 9: key = "EXCHANGE_COLLECTION_CONFIG_CN"; break;//兑换协议
                case 10: key = "USER_LOGOUT_CONFIG_CN"; break;//注销协议
            }
        }else{
            switch (type) {
                case 1: key = "HWJ_REGISTER_RISK_TERMS_CONFIG_EN"; break;
                case 2: key = "HWJ_REGISTER_PRIVACY_TERMS_CONFIG_EN"; break;
                case 3: key = "HWJ_OPENING_AGREEMENT_TERMS_CONFIG_EN"; break;
                case 4: key = "REAL_NAME_AGREEMENT_TERMS_CONFIG_EN"; break;
                case 5: key = "SERVICE_POLICY_CONFIG_EN"; break;
                case 6: key = "TRANSFER_AGREEMENT_TERMS_CONFIG_EN"; break;
                case 7: key = "INCREASE_BY_CONVERSION_TERMS_CONFIG_EN"; break;
                case 9: key = "EXCHANGE_COLLECTION_CONFIG_EN"; break;
                case 10: key = "USER_LOGOUT_CONFIG_EN"; break;
            }
        }
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    @GetMapping("/value/{key}")
    @Operation(summary =  "配置信息", description = "配置信息")
    @Parameter(name = "id", description = "配置id")
    public ServerResponseEntity<String> value(@PathVariable("key") String key){
        String value = sysConfigService.getValue(key);
        return ServerResponseEntity.success(value);
    }
}
